#!/usr/bin/env bash
# db-create-table-options -- Print Greenplum specific options for db-create-table
# $ eval "$(db-create-table-options TABLE)"
# $ db-create-table ...
##
set -eu

# for DISTRIBUTED BY clause find columns annotated with @distributed_by
distributed_by_column_names=$(jq -r '
    [ .columns
    | to_entries[]
    | select(
        [ .value.annotations[]?
        | select(.name == "distributed_by")
        ] | length > 0)
    | .key  # TODO quote/escape column names in SQL
    ] | join(", ")
    ')
[[ -z $distributed_by_column_names ]] ||
    echo 'CREATE_TABLE_EXTRA_CLAUSES+=" DISTRIBUTED BY ('"$distributed_by_column_names"')"; export CREATE_TABLE_EXTRA_CLAUSES'
